package com.app.housemanage.domain.vo;

import lombok.Data;
import java.util.List;

/**
 * 分页数据返回包装类
 */
@Data
public class PageResult<T> {
    
    /**
     * 当前页码
     */
    private Integer pageNum;
    
    /**
     * 每页大小
     */
    private Integer pageSize;
    
    /**
     * 总记录数
     */
    private Long total;
    
    /**
     * 总页数
     */
    private Integer totalPages;
    
    /**
     * 数据列表
     */
    private List<T> list;
    
    /**
     * 是否有上一页
     */
    private Boolean hasPrevious;
    
    /**
     * 是否有下一页
     */
    private Boolean hasNext;
    
    public PageResult() {}
    
    public PageResult(Integer pageNum, Integer pageSize, Long total, List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
        
        // 计算总页数
        this.totalPages = (int) Math.ceil((double) total / pageSize);
        
        // 计算是否有上一页和下一页
        this.hasPrevious = pageNum > 1;
        this.hasNext = pageNum < totalPages;
    }
    
    /**
     * 从集合创建分页结果（适用于前端分页）
     */
    public PageResult(List<T> list) {
        this.list = list;
        this.total = (long) list.size();
        this.pageNum = 1;
        this.pageSize = list.size();
        this.totalPages = 1;
        this.hasPrevious = false;
        this.hasNext = false;
    }
    
    /**
     * 从集合创建分页结果，支持自定义分页参数
     */
    public PageResult(Integer pageNum, Integer pageSize, List<T> list) {
        this.list = list;
        this.total = (long) list.size();
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        
        // 计算总页数
        this.totalPages = (int) Math.ceil((double) list.size() / pageSize);
        
        // 计算是否有上一页和下一页
        this.hasPrevious = pageNum > 1;
        this.hasNext = pageNum < totalPages;
    }
    
    /**
     * 从MyBatis Plus的Page对象创建PageResult
     */
    public static <T> PageResult<T> of(com.baomidou.mybatisplus.extension.plugins.pagination.Page<T> page) {
        return new PageResult<>(
            (int) page.getCurrent(),
            (int) page.getSize(),
            page.getTotal(),
            page.getRecords()
        );
    }
    
    /**
     * 创建空的分页结果
     */
    public static <T> PageResult<T> empty(Integer pageNum, Integer pageSize) {
        return new PageResult<>(pageNum, pageSize, 0L, java.util.Collections.emptyList());
    }
    
    /**
     * 从集合创建分页结果（单页）
     */
    public static <T> PageResult<T> of(List<T> list) {
        return new PageResult<>(list);
    }
    
    /**
     * 从集合创建分页结果，支持分页参数
     */
    public static <T> PageResult<T> of(Integer pageNum, Integer pageSize, List<T> list) {
        return new PageResult<>(pageNum, pageSize, list);
    }
    
    /**
     * 从集合创建分页结果，支持内存分页
     */
    public static <T> PageResult<T> ofMemoryPage(Integer pageNum, Integer pageSize, List<T> allList) {
        if (allList == null || allList.isEmpty()) {
            return empty(pageNum, pageSize);
        }
        
        int total = allList.size();
        int totalPages = (int) Math.ceil((double) total / pageSize);
        
        // 确保页码在有效范围内
        if (pageNum < 1) pageNum = 1;
        if (pageNum > totalPages) pageNum = totalPages;
        
        // 计算分页起始位置
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);
        
        // 获取当前页数据
        List<T> currentPageList = allList.subList(fromIndex, toIndex);
        
        return new PageResult<>(pageNum, pageSize, (long) total, currentPageList);
    }
}